// 初始化访问次数为0
var count = 232;

var likeCount = 54;

// 当页面加载完成后，执行增加访问次数的函数
window.onload = function() {
  incrementCount();
  runProgram();
  setInterval(runProgram, 1000);
};

// 增加访问次数的函数
function incrementCount() {
  // 从本地存储中获取之前的访问次数
  var storedCount = localStorage.getItem("count");
  var storedLikes = localStorage.getItem("likeCount");
  
  // 如果之前有存储过访问次数，则将其转换为数字
  if (storedCount) {
	count = parseInt(storedCount);
	
  }
  if(storedLikes){
	likeCount = parseInt(storedLikes);
  }
  
  // 增加访问次数
  count++;
  
  // 更新页面上的访问次数显示
  document.getElementById("count").innerHTML = count;
  document.getElementById("likeCount").innerHTML = likeCount;

  // 将当前的访问次数存储到本地存储中
  localStorage.setItem("count", count.toString());
  localStorage.setItem("likeCount", likeCount.toString());
}



function like() {
	var likeButton = document.getElementById("like-button");
	var likeCount = document.getElementById("likeCount");

	if (likeButton.classList.contains("liked")) {
		likeButton.classList.remove("liked");
		likeButton.innerHTML = '<img src="assets/img/visits/Black.png" alt="Like">';
		likeCount.innerText = parseInt(likeCount.innerText) - 1;
	} else {
		likeButton.classList.add("liked");
		likeButton.innerHTML = '<img src="assets/img/visits/Active.png" alt="Liked">';
		likeCount.innerText = parseInt(likeCount.innerText) + 1;
	}
	localStorage.setItem("likeCount", likeCount.innerText.toString());

}

// 开始计时
var startTime = new Date(2021, 10, 23, 0, 0, 0);

function runProgram() {
	// 执行需要计时的代码
	// 在这里插入您的代码

	// 结束计时
	const endTime = new Date();
	// 计算运行时长（以毫秒为单位）
	//const duration = new Date(endTime - startTime);
	const duration = endTime - startTime;
	//const years = duration.getFullYear();
	//const months = String(duration.getMonth() + 1).padStart(2, '0');
	//const days = String(duration.getDate()).padStart(2, '0');
	//const hours = String(duration.getHours()).padStart(2, '0');
	//const minutes = String(duration.getMinutes()).padStart(2, '0');
	//const seconds = String(duration.getSeconds()).padStart(2, '0');

	// 将毫秒转换为年、月、日、小时、分钟和秒
	const years = Math.floor(duration / 31536000000);
	const difference = duration - (31536000000 * years)
	
	//const months = Math.floor(duration / 2592000000);
	const days = Math.floor(difference / 86400000);
	const difference1 = difference - (86400000 * days)

	const hours = Math.floor(difference1 / 3600000);
	const difference2 = difference1 - (3600000 * hours)
	
	const minutes = Math.floor(difference2 / 60000);
	const difference3 = difference2 - (60000 * minutes)
	
	const seconds = Math.floor(difference3 / 1000);

	// 显示运行时长
	document.getElementById("duration").innerText = `程序运行时长：${years}年 ${days}天 ${hours}小时 ${minutes}分钟 ${seconds}秒`;

}